package com.thirteenClassicsServer.cacheService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thirteenClassics.dto.CatalogQueryDTO;
import com.thirteenClassics.entity.Catalog;
import com.thirteenClassics.entity.CatalogBook;
import com.thirteenClassicsServer.mapper.CatalogBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.List;

/**
 * @author wangzihao10 <wangzihao@kuaishou.com>
 * Created on 2024-11-05
 */
@Service
public class CatalogBookCacheService {
    @Autowired
    private CatalogBookMapper catalogBookMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    public List<Catalog> selectCatalogOrdersByNameAndId(CatalogQueryDTO catalogQueryDTO) {
        String cacheKey = "catalog:bookName:" + catalogQueryDTO.getBookName() + ":bookId:" + catalogQueryDTO.getBookId();

        // 检查缓存
        String catalogListJson = redisTemplate.opsForValue().get(cacheKey);
        if (catalogListJson != null) {
            try {
                // 从缓存中读取数据
                return JSON.parseObject(catalogListJson, new TypeReference<List<Catalog>>() {});
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 如果缓存中没有数据，则从数据库中查询
        List<Catalog> catalogList = catalogBookMapper.selectCatalogOrdersByNameAndId(catalogQueryDTO);

        // 将查询结果存入缓存
        try {
            redisTemplate.opsForValue().set(cacheKey, JSON.toJSONString(catalogList), Duration.ofDays(7));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return catalogList;
    }

    public CatalogBook getCatalogBookById(Long catalogId) {
        // 构建更具描述性的缓存键，减少冲突的可能
        String cacheKey = "catalog:book:info:" + catalogId;

        // 从缓存中读取数据
        String cachedCatalogBookJson = stringRedisTemplate.opsForValue().get(cacheKey);
        if (cachedCatalogBookJson != null) {
            try {
                // 将缓存中的 JSON 字符串反序列化为 CatalogBook 对象
                return objectMapper.readValue(cachedCatalogBookJson, CatalogBook.class);
            } catch (Exception e) {
                // 处理反序列化异常
                e.printStackTrace();
            }
        }

        // 如果缓存中没有数据，则从数据库中查询
        CatalogBook catalogBook = catalogBookMapper.selectCatalogBookAllMinorityById(catalogId);

        // 将查询结果存入缓存，转换为 JSON 字符串
        if (catalogBook != null) {
            try {
                String catalogBookJson = objectMapper.writeValueAsString(catalogBook);
                stringRedisTemplate.opsForValue().set(cacheKey, catalogBookJson, Duration.ofDays(7));
            } catch (Exception e) {
                // 处理序列化异常
                e.printStackTrace();
            }
        }

        return catalogBook;
    }
}
